package com.salt.exception;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class ReturnMessageBean<T> {
    private boolean success;

    private String errorCode;

    private String errorMessage;

    private T data;

    @JsonIgnore
    private String orderNumber;


    public static String success(String data) {
        ReturnMessageBean message = new ReturnMessageBean();
        message.setSuccess(true);
        message.setData(data);
        message.setErrorCode(null);
        message.setErrorMessage(null);
        return JSON.toJSONString(message);
    }

    public static ReturnMessageBean success() {
        ReturnMessageBean message = new ReturnMessageBean();
        message.setSuccess(true);
        message.setData(null);
        message.setErrorCode(null);
        message.setErrorMessage(null);
        return message;
    }

    public static <T> ReturnMessageBean successMessageBean(T data) {
        ReturnMessageBean message = new ReturnMessageBean();
        message.setSuccess(true);
        message.setData(data);
        message.setErrorMessage("操作成功");
        message.setErrorCode("200");
        return message;
    }


    public static String error(String errorMessage) {
        ReturnMessageBean message = new ReturnMessageBean();
        message.setSuccess(false);
        message.setData(null);
        message.setErrorCode("500");
        message.setErrorMessage(errorMessage);
        return JSON.toJSONString(message);
    }


    public static ReturnMessageBean errorMessageBean(String errorMessage) {
        ReturnMessageBean message = new ReturnMessageBean();
        message.setSuccess(false);
        message.setData(null);
        message.setErrorCode(null);
        message.setErrorMessage(errorMessage);
        return message;
    }

    public static ReturnMessageBean errorMessageBean(String errorMessage,String errorCode) {
        ReturnMessageBean message = new ReturnMessageBean();
        message.setSuccess(false);
        message.setData(null);
        message.setErrorCode(errorCode);
        message.setErrorMessage(errorMessage);
        return message;
    }

    public String toJson(){
        return "{\"success\":"+this.success+",\"errorCode\":"+this.errorCode+",\"errorMessage\":\""+this.errorMessage+"\",\"data\":"+getData()+"}";
    }

    public T getData() {
        if(data!= null &&
                data instanceof String &&
                StringUtils.isEmpty((String) data)){
            data = null;
        }
        return data;
    }
}
